package org.example.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 用户数据访问层
 */
@Mapper
public interface UserRepository extends BaseMapper<User> {
    
    /**
     * 根据用户名查找用户
     */
    @Select("SELECT * FROM users WHERE username = #{username}")
    User findByUsername(@Param("username") String username);
    
    /**
     * 根据邮箱查找用户
     */
    @Select("SELECT * FROM users WHERE email = #{email}")
    User findByEmail(@Param("email") String email);
    
    /**
     * 根据用户名模糊查询
     */
    @Select("SELECT * FROM users WHERE username LIKE CONCAT('%', #{username}, '%')")
    List<User> findByUsernameContaining(@Param("username") String username);
    
    /**
     * 统计用户数量
     */
    @Select("SELECT COUNT(*) FROM users")
    long countUsers();
    
    /**
     * 根据用户名和邮箱查询用户
     */
    @Select("SELECT * FROM users WHERE username = #{username} AND email = #{email}")
    User findByUsernameAndEmail(@Param("username") String username, @Param("email") String email);
}